package com.fanrui.code.util;

import java.io.Reader;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.fanrui.code.util.core.PropertiesUtil;

public class SessionUtil {
	
	private static SqlSessionFactory sqlSessionFactory;

	static {
		try {
			Reader reader = PropertiesUtil.getResourceAsReader("Configuration.xml");
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			e.printStackTrace();	
		}
	}
	
	public static SqlSessionFactory getSessionFactory() {
		return sqlSessionFactory;
	}
	
	public static SqlSession getSession() {
		return sqlSessionFactory.openSession(true);
	}
	
	public static <T> T  getMapper(Class<T> c){
		return getSession().getMapper(c);
	}
}
